package com.shadt.core.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.shadt.core.model.Json;
import com.shadt.core.service.RoleService;
import com.shadt.core.vo.RoleVo;

/**
 * 角色控制器
 * 
 * @author SongR
 *
 */
@Controller
@RequestMapping("/roleAction")
public class RoleController {
	@Autowired
	RoleService roleService;
	
	/**
	 * 获取角色
	 * @param requert
	 * @param session
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getRole")
	public Json getRole(HttpServletRequest requert,HttpSession session,String id){
		System.out.println();
		Json j = new Json();
		List<RoleVo> roleVo = roleService.getRole(id); 
//		for (RoleVo roleVo2 : roleVo) {
//			System.out.println(roleVo2.getName());
//		}
		j.setObj(roleVo);
		j.setMsg("获取成功！");
		return j;
	}
	/**
	 * 用户授权
	 * @param request
	 * @param session
	 * @param ids
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/addEmpower")
	public Json addEmpower(HttpServletRequest request,HttpSession session,String roleIds,String userId){
		Json j = new Json();
		try {
			roleService.addRole(roleIds,userId);
			j.setSuccess(true);
			j.setMsg("授权成功！");
		} catch (Exception e) {
			j.setMsg("授权失败！请联系管理员！");
			e.printStackTrace();
		}
		return j;
	}
	/**
	 * 跳转到角色管理页面
	 * @return
	 */
	@RequestMapping("/toRolePage")
	public String toRolePage(){
		return "/page/rolemanager";
	}
	/**
	 * 添加角色
	 * @return
	 */
	@RequestMapping("/addRole")
	@ResponseBody
	public Json addRole(RoleVo roleVo){
//		System.out.println("addRole--->");
		Json j = new Json();
		try {
			roleService.addRole(roleVo);
			j.setMsg("添加成功！");
			j.setSuccess(true);
			j.setObj(roleVo);
		} catch (Exception e) {
			j.setMsg("添加失败！请联系管理员！");
			e.printStackTrace();
		}
//		System.out.println("addResource-->");
		return j;
	}
	
	/**
	 * 获取所有角色
	 * @return
	 */
	@RequestMapping("/getAllRole")
	@ResponseBody
	public String getAllRole(HttpServletRequest request, String name){
		System.out.println("getAllRole--->");
		String names = request.getParameter("aoData");
		JSONArray jsonarray = JSONArray.parseArray(names);
		String sEcho = "", iDisplayStart = "", iDisplayLength = "", sSearch = "";
		for (int i = 0; i < jsonarray.size(); i++) {
			JSONObject obj = (JSONObject) jsonarray.get(i);
			if (obj.get("name").equals("sEcho"))
				sEcho = obj.get("value").toString();
			if (obj.get("name").equals("iDisplayStart")) // 开始显示行
				iDisplayStart = obj.get("value").toString();
			if (obj.get("name").equals("iDisplayLength")) // 每页显示的长度
				iDisplayLength = obj.get("value").toString();
			if (obj.get("name").equals("sSearch"))
				sSearch = obj.get("value").toString();
		}
		List<RoleVo> resList = roleService.getAllRole(sSearch,
				Integer.parseInt(iDisplayStart),
				Integer.parseInt(iDisplayLength));
		String aaData = JSONArray.toJSONString(resList).toString();
		String json = "{\"sEcho\":" + sEcho + ",\"iTotalRecords\":"
				+ roleService.getRoleCount(sSearch)
				+ ",\"iTotalDisplayRecords\":"
				+ roleService.getRoleCount(sSearch) + ",\"aaData\":" + aaData
				+ "}";
		return json;
	}  
	
}
